FileConvert: Mobile File Format Conversion

更新时间:
2024-07-01
下载文档

FileConvert: Mobile File Format Conversion

The fileConvert module provides many interfaces for applications to convert file formats.

Developers can determine whether these interfaces work in the EdgerOS mobile App environment through the following code:

edger.env().then(data => {
  if (data.env === 'edgerapp') {
    // We work in EdgerOS mobile App environment.
  }
}).catch(error => {
  console.error(error)
})

Functions

edger.fileConvert.convertPdf(params)

Convert the file format from doc, docx, ppt, and pptx to pdf format.

  • params {Object} Parameters for converting file format to pdf format.
  • Returns: {Promise}.

The params is an object, it can contain the following members:

  • convertTaskId {String} The unique id of the conversion task.
  • url {String} The url of file. The iOS system does not support converting local files. The Android system can only convert URLs to local files in the format of file://${path}.
  • fileId {String} The unique id of file. Optional.
  • fileSize {String} The size of file.
  • type {String} The type of file. Optional values: doc | docx | ppt | pptx.
  • targetConvertType {String} The converted file type. Only supports pdf.
  • createTime {number} The file's creation time, in timestamp format. Optional.
  • modifyTime {number} The file's modification time, in timestamp format. Optional.
  • headers {Object} The request headers when requesting the file URL. Optional.

After successful conversion, get an object of result, the object includes:

  • convertTaskId {String} The unique id of the conversion task.
  • url {String} The url of file. By requesting this URL, you can obtain the converted file data, or retrieve the cached path of the converted file through the path parameter in the URL, enabling preview, upload, and other operations.
  • fileSize {String} The size of file.
  • supportAjaxGet {Boolean} Whether to support Ajax request to obtain converted files. If false, Ajax request is not supported and needs to be uploaded through job module. For details, please refer to Job: Mobile Job Management.

After conversion failed, get some error messages:

Error MessageError Reason
10001File download failed
10002Original file type not support
10003File convert type not support
10004File too large not support
10005File convert fail

Example

const params = {
  convertTaskId: '23t3-nc43-scss-q139',
  targetConvertType: 'pdf',
  url: 'https://host/path/to/your/file.pptx',
  type: 'pptx',
  fileSize: 34930293
}

edger.fileConvert.convertPdf(params).then((res) => {
  console.log("convert file to pdf successful.", res)
}).catch((error) => {
  console.error(error)
})

async / await

async function convertPdf() {
  try {
    const res = await edger.fileConvert.convertPdf({
      convertTaskId: '23t3-nc43-scss-q139',
      targetConvertType: 'pdf',
      url: 'https://host/path/to/your/file.pptx', 
      type: 'pptx',
      fileSize: 34930293
    })
    console.log('convert file to pdf successful.', res)

    // case 1: Get the arrayBuffer of the converted file
    const response = await fetch(res.url)
    const arrayBuffer = await response.arrayBuffer()
    console.log('converted pdf arrayBuffer.', arrayBuffer)

    // case 2: Preview converted files
    const parsedURL = new URL(res.url)
    const searchParams = new URLSearchParams(parsedURL.search)
    const path = searchParams.get('path') || ''
    const file = {
          id: '003r-3442-ewf3-dss5',
          name: fileName,
          type: type,
          url: `file://${path}`
        }
    const previewRes = await edger.filePreview.open({ files: [file] })
    console.log('preview converted pdf successful.', previewRes)

    // case 3: Job upload converted files
    const jobRes = await edger.job.create('job_transport_upload_form', {
      url: `${window.location.origin}/api/upload`,
      headers: { ha: 'ha' },
      params: { hp: 'hp' },
      tasks: [{
          filePath: path,
          fileType: 'file',
          fileName: `${fileName}.${type}`
        }]
      },
      { timeout: 100, wifiOnly: false, tags: ['myjob'] }
    )
    console.log('job upload converted pdf successful.', jobRes)
  } catch (error) {
    console.error(error)
  }
}

edger.fileConvert.cancelConvertPdf(params)

Cancel the file conversion to pdf.

  • convertTaskId {String} The unique id of the conversion task.
  • Returns: {Promise}.

Example

const params = {
  convertTaskId: '23t3-nc43-scss-q139'
}

edger.fileConvert.cancelConvertPdf(params).then((res) => {
  console.log('cancel convert file to pdf successful.', res)
}).catch((error) => {
  console.error(error)
})

async / await

async function cancelConvertPdf() {
  try {
    const res = await edger.fileConvert.cancelConvertPdf({ convertTaskId: '23t3-nc43-scss-q139' })
    console.log('cancel convert file to pdf successful.', res)
  } catch (error) {
    console.error(error)
  }
}

edger.fileConvert.convertImage(params)

Convert pdf to multiple images.

  • params {Object} Parameters for converting pdf to multiple images.
  • Returns: {Promise}.

The params is an object, it can contain the following members:

  • convertTaskId {String} The unique id of the conversion task.
  • url {String} The url of file. The iOS system does not support converting local files. The Android system can only convert URLs to local files in the format of file://${path}.
  • fileId {String} The unique id of file. Optional.
  • fileSize {String} The size of file.
  • type {String} The type of file. Only supports pdf.
  • targetConvertType {String} The converted file type. Only supports jpeg.
  • createTime {number} The file's creation time, in timestamp format. Optional.
  • modifyTime {number} The file's modification time, in timestamp format. Optional.
  • headers {Object} The request headers when requesting the file URL. Optional.
  • targetWidth {number} The resolution size of the image to be generated typically corresponds to the physical width pixel size of the displaying device. The width range of image is from 0 to 3840.
  • targetQuality {number} The quality of the image to be generated. The quality range of image is from 0 to 100.

After successful conversion, get an object of result, the object includes:

  • convertTaskId {String} The unique id of the conversion task.
  • url {Array} The collection of images obtained after converting PDF.

After conversion failed, get some error messages:

Error MessageError Reason
10001File download failed
10004File too large not support
10005File convert fail
10006Params not support

Example

const params = {
  convertTaskId: '23t3-nc43-scss-q139',
  targetConvertType: 'jpeg',
  url: 'https://host/path/to/your/file.pdf',
  type: 'pdf',
  fileSize: 34930293,
  targetWidth: 2048,
  targetQuality: 100
}

edger.fileConvert.convertImage(params).then((res) => {
  console.log("convert pdf to image successful.", res)
}).catch((error) => {
  console.error(error)
})

async / await

async function convertImage() {
  try {
    const res = await edger.fileConvert.convertImage({
      convertTaskId: '23t3-nc43-scss-q139',
      targetConvertType: 'jpeg',
      url: 'https://host/path/to/your/file.pdf',
      type: 'pdf',
      fileSize: 34930293,
      targetWidth: 2048,
      targetQuality: 100
    })
    console.log("convert pdf to image successful.", res)
  } catch (error) {
    console.error(error)
  }
}

edger.fileConvert.cancelConvertImage(params)

Cancel the pdf conversion to images.

  • convertTaskId {String} The unique id of the conversion task.
  • Returns: {Promise}.

Example

const params = {
  convertTaskId: '23t3-nc43-scss-q139'
}

edger.fileConvert.cancelConvertImage(params).then((res) => {
  console.log('cancel convert pdf to images successful.', res)
}).catch((error) => {
  console.error(error)
})

async / await

async function cancelConvertImage() {
  try {
    const res = await edger.fileConvert.cancelConvertImage({ convertTaskId: '23t3-nc43-scss-q139' })
    console.log('cancel convert pdf to images successful.', res)
  } catch (error) {
    console.error(error)
  }
}

Events

The unified event listener provided by Web-SDK:

const listener = (payload) => {
  // Event handling...
}

// add listener
edger.fileConvert.addEventListener('some-event', listener);

// or 
// onAction() is an alias of addEventListener().
edger.fileConvert.onAction('some-event', listener);

// remove listener
edger.fileConvert.removeEventListener('some-event', listener);

// remove all listeners
edger.fileConvert.removeAllListeners();

progress

This event will be generated during the file conversion process.

  • Returns: {Object}.

Get an object of result, the object includes:

  • convertTaskId {String} The unique id of the conversion task.
  • progress {number} The file conversion progress.

Example

edger.fileConvert.addEventListener('progress', (payload) => {
  const { convertTaskId, progress } = payload;
  console.log("current file convert progress:", convertTaskId, progress);
});
文档内容是否对您有所帮助?
有帮助
没帮助